home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / Rotato / Source / ControlPanel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  10.5 KB  |  567 lines  |  [TEXT/CWIE]

  1.  
  2. #include <DeskBus.h>
  3.  
  4.  
  5. #include "PrefResource.h"
  6.  
  7. #define kPi        3.1415926535898
  8.  
  9.  
  10. #define kMouseDialogResID        128
  11.  
  12. enum
  13. {
  14.     eRotationSliderID = 1,
  15.     eRotationPicID = 3,
  16.     eAutosenseCheckboxID
  17. };
  18.  
  19.  
  20. enum
  21. {
  22.     eBasePicID = 200
  23.  
  24. };
  25.  
  26. Boolean gQuitFlag = false;
  27.  
  28.  
  29. enum {
  30.     kAppleMenuID = 128,
  31.     kFileMenuID,
  32.     kEditMenuID
  33. };
  34.  
  35. enum {
  36.     kAboutMenuItem        = 1
  37. };
  38.  
  39. enum {
  40.     kCloseMenuItem        = 1,
  41.     kQuitMenuItem
  42. };
  43.  
  44. static MenuHandle    gAppleMenu, gFileMenu, gEditMenu;
  45.  
  46.  
  47. /*----------------------------------------------------------------------------
  48.     GetMousePrefs 
  49.     
  50. ----------------------------------------------------------------------------*/
  51. static OSErr GetMousePrefs(TPrefsData *outPrefsData)
  52. {
  53.     TPrefsDataPtr        globalDataPtr;
  54.     OSErr            err;
  55.     
  56.     err = Gestalt(kDataGestaltSelector, (long *)&globalDataPtr);
  57.     if (err != noErr) return err;
  58.     
  59.     *outPrefsData = *globalDataPtr;
  60.     return noErr;
  61. }
  62.  
  63. /*----------------------------------------------------------------------------
  64.     SetMousePrefs 
  65.     
  66. ----------------------------------------------------------------------------*/
  67.  
  68. static OSErr SetMousePrefs(double thetaDelta)
  69. {
  70.     TPrefsDataPtr        globalDataPtr;
  71.     OSErr            err;
  72.     
  73.     err = Gestalt(kDataGestaltSelector, (long *)&globalDataPtr);
  74.     if (err != noErr) return err;
  75.  
  76.     globalDataPtr->thetaDelta = thetaDelta;
  77.     return noErr;
  78. }
  79.  
  80.  
  81. //------------------------DrawBeast--------------------
  82. static pascal void DrawMousePicture (DialogPtr theDialog, short itemNo)
  83. {
  84.     PicHandle        mousePic;
  85.     short        itemType, whichSetting;
  86.     short        picID;
  87.     Rect            box;
  88.     Handle        item;
  89.  
  90. //Find which beast
  91.     GetDialogItem(theDialog, eRotationSliderID, &itemType, &item, &box);
  92.     whichSetting = GetControlValue((ControlHandle)item);
  93.     
  94.     picID = eBasePicID + whichSetting - 1;
  95.     
  96.     GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
  97.     
  98.     mousePic = GetPicture(picID);
  99.     if (mousePic)
  100.     {
  101.         HNoPurge((Handle)mousePic);
  102.         DrawPicture(mousePic, &box);
  103.         HPurge((Handle)mousePic);
  104.         ReleaseResource((Handle)mousePic);
  105.     }
  106. }
  107.  
  108.  
  109. #pragma mark -
  110.  
  111. /*----------------------------------------------------------------------------
  112.     DisplayMouseDialog 
  113.     
  114. ----------------------------------------------------------------------------*/
  115.  
  116. static OSErr DisplayMouseDialog(TPrefsData *ioPrefs)
  117. {
  118.     DialogPtr        dlg;
  119.     Rect            box;
  120.     short        itemType;
  121.     short        controlValue = 3;
  122.     double        thetaDelta;
  123.     Handle        itemHandle;
  124.     UserItemUPP    picDrawProc;
  125.     
  126.     dlg = GetNewDialog(kMouseDialogResID, nil, (WindowPtr)-1);
  127.     if (!dlg) return resNotFound;
  128.  
  129.     picDrawProc = NewUserItemProc(DrawMousePicture);
  130.     
  131.     GetDialogItem(dlg, eRotationPicID, &itemType, &itemHandle, &box);
  132.     SetDialogItem(dlg, eRotationPicID, itemType, (Handle)picDrawProc, &box);
  133.  
  134.     thetaDelta = ioPrefs->thetaDelta;
  135.     
  136.     if (thetaDelta <= -kPi / 4.0)
  137.         controlValue = 5;
  138.     else if (thetaDelta <= -kPi / 8.0)
  139.         controlValue = 4;
  140.     else if (thetaDelta <= 0.0)
  141.         controlValue = 3;
  142.     else if (thetaDelta <= kPi / 8.0)
  143.         controlValue = 2;
  144.     else
  145.         controlValue = 1;
  146.  
  147.     GetDialogItem(dlg, eRotationSliderID, &itemType, &itemHandle, &box);
  148.     SetControlValue((ControlHandle)itemHandle, controlValue);
  149.  
  150.     ShowWindow(dlg);    
  151.     return noErr;
  152. }
  153.  
  154.  
  155.  
  156. static void ShowAboutBox(void)
  157. {
  158.     SysBeep(2);
  159. }
  160.  
  161.  
  162.  
  163. #pragma mark -
  164.  
  165. /*-------------------------DoIdleEvent-------------------------*/
  166. static Boolean HandleDialogEvent(WindowPtr dlg, short itemHit)
  167. {
  168.     Rect            box;
  169.     Handle        itemHandle;
  170.     short        itemType;
  171.     GrafPtr        oldPort;
  172.     Boolean        handled = false;
  173.     
  174.     GetPort(&oldPort);
  175.     SetPort(dlg);
  176.     
  177.     switch (itemHit)
  178.     {
  179.         case eRotationSliderID:
  180.         {
  181.             short    whichSetting;
  182.             double    theta = 0.0;
  183.             
  184.             GetDialogItem(dlg, eRotationPicID, &itemType, &itemHandle, &box);
  185.             InvalRect(&box);
  186.             
  187.             GetDialogItem(dlg, eRotationSliderID, &itemType, &itemHandle, &box);
  188.             whichSetting = GetControlValue((ControlHandle)itemHandle);
  189.             
  190.             switch (whichSetting)
  191.             {
  192.                 case 1:            // -60
  193.                     theta = kPi / 4.0;
  194.                     break;
  195.                 case 2:
  196.                     theta = kPi / 8.0;
  197.                     break;
  198.                 default:
  199.                 case 3:
  200.                     theta = 0.0;
  201.                     break;
  202.                 case 4:
  203.                     theta = -kPi / 8.0;
  204.                     break;
  205.                 case 5:
  206.                     theta = -kPi / 4.0;
  207.                     break;
  208.             }
  209.             
  210.             SetMousePrefs(theta);
  211.         }
  212.         break;
  213.         
  214.     case eAutosenseCheckboxID:
  215.         {
  216.             ControlHandle    control;
  217.             short        curValue;
  218.             
  219.             GetDialogItem(dlg, eAutosenseCheckboxID, &itemType, &(Handle)control, &box);
  220.             curValue = GetControlValue(control);
  221.             SetControlValue(control, !curValue);
  222.         }
  223.         break;
  224.         
  225.     }
  226.         
  227.     SetPort(oldPort);
  228.     return handled;
  229. }
  230.  
  231.  
  232. /*-------------------------DoIdleEvent-------------------------*/
  233. static void DoIdleEvent(void)
  234. {
  235.  
  236.  
  237. }
  238.  
  239.  
  240. /*-------------------------DoUpdateEvent-------------------------*/
  241. static void HandleWindowUpdate(WindowPtr wind)
  242. {
  243.     
  244.  
  245. }
  246.  
  247.  
  248. /*-------------------------DoUpdateEvent-------------------------*/
  249. static Boolean DoClose(WindowPtr wind)
  250. {
  251.     CloseWindow(wind);
  252.     return true;
  253. }
  254.  
  255. /*-------------------------DoUpdateEvent-------------------------*/
  256. static void DoUpdateEvent(EventRecord localEvent)
  257. {
  258.     WindowPtr     wind;
  259.     GrafPtr        oldPort;
  260.     
  261.     GetPort(&oldPort);
  262.     
  263.     wind = (WindowPtr)localEvent.message;
  264.     
  265.     SetPort(wind);
  266.     BeginUpdate(wind);
  267.  
  268.     HandleWindowUpdate(wind);
  269.     
  270.     EndUpdate(wind);
  271.     SetPort(oldPort);
  272. }
  273.  
  274.  
  275. /*-------------------------DoMenuSelection-------------------------*/
  276. static Boolean DoMenuSelection (long retVal)
  277. {
  278.     WindowPtr    wind;
  279.     short        menuID, itemID;
  280.     Str255        itemStr;
  281.     OSErr         err = noErr;
  282.     Boolean        returnVal = false;
  283.     
  284.     wind = FrontWindow();
  285.     
  286.     menuID = HiWord (retVal);
  287.     itemID = LoWord (retVal);
  288.     
  289.     switch (menuID) {
  290.         case kAppleMenuID:
  291.             if (itemID == kAboutMenuItem)
  292.                 ShowAboutBox ();    /*    Show the about box */
  293.             else {
  294.                 GetMenuItemText(GetMenuHandle(kAppleMenuID), itemID, itemStr);
  295.                 OpenDeskAcc(itemStr);
  296.             }
  297.             break;
  298.             
  299.         case kFileMenuID:
  300.             switch (itemID)
  301.             {
  302.                 case kCloseMenuItem:
  303.                     returnVal = DoClose(wind);
  304.                     break;
  305.                     
  306.                 case kQuitMenuItem:
  307.                     returnVal = true;
  308.                     break;
  309.                     
  310.                 default:
  311.                     break;
  312.             }
  313.             break;
  314.         case kEditMenuID:
  315.             switch (itemID)
  316.             {
  317.                 default:
  318.                     break;
  319.             }
  320.             break;
  321.         
  322.         default:
  323.             break;
  324.     }
  325.     
  326.     HiliteMenu(0);
  327.     
  328.     return returnVal;
  329. }
  330.  
  331.  
  332.  
  333. /*-------------------------DoActivateEvent-------------------------*/
  334. static void DoActivateEvent(EventRecord localEvent)
  335. {
  336.     Boolean activating;
  337.     
  338.     activating = (localEvent.modifiers & activeFlag) != 0;
  339. }
  340.  
  341.  
  342. /*-------------------------DoOSEvent-------------------------*/
  343. static void DoOSEvent(EventRecord localEvent)
  344. {
  345.     Boolean  resuming;
  346.     
  347.     if (((localEvent.message >> 24) & 0xFF) != suspendResumeMessage) return;
  348.  
  349.     resuming = (localEvent.message & 1) == resumeFlag;
  350. }
  351.  
  352.  
  353.  
  354. /*-------------------------HandleMouseDown-------------------------*/
  355. static Boolean HandleMouseDown(EventRecord theEvent)
  356. {
  357.     WindowPtr    whichWindow;
  358.     Point            where;
  359.     short        whichPart;
  360.     Rect            boundsRect;
  361.     GrafPtr        oldPort;
  362.     OSErr        err = noErr;
  363.     Boolean        returnVal = false;
  364.     
  365.     whichPart = FindWindow (theEvent.where, &whichWindow);
  366.     where = theEvent.where;
  367.     
  368.     GetPort(&oldPort);
  369.     SetPort(whichWindow);
  370.     
  371.     GlobalToLocal(&where);
  372.     
  373.     switch (whichPart)
  374.     {
  375.         case inMenuBar:
  376.             returnVal = DoMenuSelection(MenuSelect (theEvent.where));
  377.             break;
  378.         
  379.         case inSysWindow:
  380.             SystemClick (&theEvent, whichWindow);
  381.             break;
  382.         
  383.         case inDrag:
  384.             boundsRect = (*GetGrayRgn())->rgnBBox;
  385.             DragWindow(whichWindow, theEvent.where, &boundsRect);
  386.             break;
  387.         
  388.         case inGoAway:
  389.             if (TrackGoAway(whichWindow, where))
  390.                 returnVal = DoClose(whichWindow);
  391.             break;
  392.             
  393.         case inGrow:
  394.             break;
  395.             
  396.         case inZoomIn:
  397.         case inZoomOut:
  398.             break;
  399.             
  400.         case inContent:
  401.             SelectWindow(whichWindow);
  402.         default:
  403.             break;
  404.         }
  405.  
  406.     SetPort(oldPort);
  407.     
  408.     return returnVal;
  409. }
  410.  
  411. /*-------------------------HandleKeyDown-------------------------*/
  412. /*    Handle key presses. Check to see if it's a command key, and if so,
  413.     handle it and return 0.
  414. */
  415. static Boolean HandleKeyDown(EventRecord theEvent)
  416. {
  417.     if (theEvent.modifiers & cmdKey)
  418.         return DoMenuSelection (MenuKey ((char)theEvent.message & charCodeMask));
  419.  
  420.     return false;
  421. }
  422.  
  423. /*-------------------------DoEventLoop-------------------------*/
  424. static void DoEventLoop(Boolean *outOptionKey)
  425. {
  426.     EventRecord    localEvent;
  427.     
  428.     while (!gQuitFlag)
  429.     {
  430.         if (WaitNextEvent(everyEvent, &localEvent, 20, nil))
  431.         {
  432.             
  433.             if (IsDialogEvent(&localEvent))
  434.             {
  435.                 DialogPtr        dlg;
  436.                 short        itemHit;
  437.                 
  438.                 if (DialogSelect(&localEvent, &dlg, &itemHit))
  439.                 {
  440.                     HandleDialogEvent(dlg, itemHit);
  441.                     continue;
  442.                 }
  443.             }
  444.             
  445.             switch (localEvent.what)
  446.             {
  447.                 case mouseDown:
  448.                     gQuitFlag = HandleMouseDown(localEvent);
  449.                     break;
  450.                     
  451.                 case keyDown:
  452.                 case autoKey:
  453.                     gQuitFlag = HandleKeyDown(localEvent);
  454.                     break;
  455.  
  456.                 case diskEvt:
  457.                     /*Handle a disk insert. Checks for duff disks*/
  458.                     if (HiWord(localEvent.message)) {
  459.                         Point diskInitPt;
  460.                         
  461.                         diskInitPt.v = diskInitPt.h = 100;
  462.                         DILoad();
  463.                         DIBadMount(diskInitPt, localEvent.message);
  464.                         DIUnload();
  465.                     }
  466.                     break;
  467.                 
  468.                 case updateEvt:
  469.                     DoUpdateEvent(localEvent);
  470.                     break;    /*Do nothing*/
  471.                 
  472.                 case activateEvt:
  473.                     DoActivateEvent(localEvent);
  474.                     break;
  475.                 
  476.                 case osEvt:
  477.                     DoOSEvent(localEvent);
  478.                     break;
  479.                     
  480.                 case nullEvent:
  481.                     DoIdleEvent();
  482.                     break;    /*Do nothing*/
  483.                     
  484.                 case kHighLevelEvent:
  485.                     //DoHighLevelEvent(localEvent);
  486.                     break;
  487.                     
  488.                 default:
  489.                     break;    /*Do nothing*/
  490.  
  491.             }    /*Switch*/
  492.         } else {
  493.             DoIdleEvent();
  494.         }
  495.         
  496.     }    /*while*/
  497.     
  498.     *outOptionKey = (localEvent.modifiers & optionKey) != 0;
  499. }
  500.  
  501.  
  502. /*-------------------------SetupMenus-------------------------*/
  503. static void SetupMenus (void)
  504. {
  505.  
  506.     gAppleMenu = GetMenu (kAppleMenuID);
  507.     AppendResMenu (gAppleMenu, 'DRVR' );
  508.     InsertMenu (gAppleMenu, 0);
  509.  
  510.     gFileMenu = GetMenu ( kFileMenuID );
  511.     InsertMenu (gFileMenu, 0);
  512.     
  513.     gEditMenu = GetMenu ( kEditMenuID );
  514.     InsertMenu (gEditMenu, 0);
  515.             
  516.     DrawMenuBar ();
  517. }
  518.  
  519. /*-------------------------InitToolbox-------------------------*/
  520. static void InitToolbox (void)
  521. {
  522.     InitGraf ( &qd.thePort );
  523.     InitFonts ();
  524.     InitWindows ();
  525.     InitMenus ();
  526.     TEInit ();
  527.     InitDialogs (NULL);
  528.     InitCursor ();
  529.     FlushEvents ( everyEvent, 0 );
  530.  
  531.     MoreMasters ();
  532.     MoreMasters ();
  533. }
  534.  
  535.  
  536. /*----------------------------------------------------------------------------
  537.     main 
  538.     
  539. ----------------------------------------------------------------------------*/
  540.  
  541. void main(void)
  542. {
  543.     TPrefsData    prefsData = {0};
  544.     Boolean        optionKeyOnQuit = false;
  545.     OSErr        err = noErr;
  546.     
  547.     InitToolbox();
  548.     SetupMenus();
  549.  
  550.     err = GetMousePrefs(&prefsData);
  551.     if (err != noErr) return;
  552.     
  553.     // this sets the data live, so no need to save it out.
  554.     DisplayMouseDialog(&prefsData);
  555.     
  556.     DoEventLoop(&optionKeyOnQuit);
  557.     
  558.     if (optionKeyOnQuit)        // let's be really evil,and choose a random angle
  559.     {
  560.         double    randNum = (double)Random() / 32767.0;
  561.         double    randomAngle = kPi * randNum;
  562.         
  563.         SetMousePrefs(randomAngle);
  564.     }
  565. }
  566.  
  567.